<?php

namespace app\merchant\controller;

use app\merchant\model\Order as OrderModel;
use app\merchant\model\Express as ExpressModel;
use app\merchant\model\GiftsOrder as GiftsOrderModel;
use app\store\model\store\shop\Clerk as ShopClerkModel;


/**
 * 订单管理类
 * Class Order
 * @package app\merchant\controller
 */
class Order extends Base
{

    /**
     * 订单列表
     * @param string $title
     * @param string $dataType
     * @return mixed
     * @throws \think\exception\DbException
     */
    private function getList($dataType)
    {
        // 订单列表
        $model = new OrderModel;
        $list = $model->getList($dataType, $this->request->param());

        return $this->renderSuccess($list,"查询成功");

    }


    /**
     * 全部订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function all_list()
    {
        return $this->getList('all');
    }


    /**
     * 待发货订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function delivery_list()
    {
        return $this->getList('delivery');
    }

    /**
     * 待收货订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function receipt_list()
    {
        return $this->getList('receipt');
    }

    /**
     * 待付款订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function pay_list()
    {
        return $this->getList( 'pay');
    }

    /**
     * 已完成订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function complete_list()
    {
        return $this->getList('complete');
    }

    /**
     * 已取消订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function cancel_list()
    {
        return $this->getList('cancel');
    }


    /**
     * 订单详情
     * @param $order_id
     * @param $order_source 订单类型 5 爆品礼 10普通 20砍价 30秒杀 40购买储值卡 50当面付
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $param = $this->postData();
        if(!isset($param['order_id']) || isInt($param['order_id'])){
            return $this->renderError([],'参数错误');
        }
        if(!isset($param['order_source']) || isInt($param['order_source'])){
            return $this->renderError([],'参数错误');
        }

        //爆品订单
        if($param['order_source']==5){
            $detail = GiftsOrderModel::detail($param['order_id']);
            if(!$detail){
                return $this->renderError([],'订单不存在，请刷新重试');
            }

            return $this->renderSuccess(compact(
                'detail'
            ),'查询成功');

        }elseif($param['order_source']==10 || $param['order_source']==20 || $param['order_source']==30){
            // 订单详情
            $filter = ['order_id'=>$param['order_id'],'order_source'=>$param['order_source']];
            $detail = OrderModel::detail($filter);
            if(!$detail){
                return $this->renderError([],'订单不存在，请刷新重试');
            }
            // 物流公司列表
            $expressList = ExpressModel::getAll();
            // 核销员列表
            $shopClerkList = (new ShopClerkModel)->getList(true);

            return $this->renderSuccess(compact(
                'detail',
                'expressList',
                'shopClerkList'
            ),'查询成功');

        }elseif($param['order_source']==40){
            // 订单详情
            $filter = ['order_id'=>$param['order_id'],'order_source'=>$param['order_source']];
            $detail = OrderModel::cardDetail($filter);
            if(!$detail){
                return $this->renderError([],'订单不存在，请刷新重试');
            }

            return $this->renderSuccess(compact(
                'detail'
            ),'查询成功');

        }elseif($param['order_source']==50){
            // 订单详情
            $filter = ['order_id'=>$param['order_id'],'order_source'=>$param['order_source']];
            $detail = OrderModel::faceDetail($filter);
            if(!$detail){
                return $this->renderError([],'订单不存在，请刷新重试');
            }

            return $this->renderSuccess(compact(
                'detail'
            ),'查询成功');
        }


    }

    /**
     * 确认发货
     * @param $order_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function delivery()
    {
        $param = $this->postData();
        if(!isset($param['order_id']) || isInt($param['order_id'])){
            return $this->renderError([],'参数错误');
        }
        if(!isset($param['express_id']) || isInt($param['express_id'])){
            return $this->renderError([],'请选择物流公司');
        }
        if(!isset($param['express_no']) || empty($param['express_no'])){
            return $this->renderError([],'请输入物流单号');
        }

        $model = OrderModel::detail($param['order_id']);
        if ($model->delivery($param)) {
            return $this->renderSuccess([],'发货成功');
        }
        return $this->renderError([],$model->getError() ?: '发货失败');
    }


    /**
     * 修改订单价格
     * @param $order_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function updatePrice($order_id)
    {
        $model = OrderModel::detail($order_id);
        if ($model->updatePrice($this->postData('order'))) {
            return $this->renderSuccess('修改成功');
        }
        return $this->renderError($model->getError() ?: '修改失败');
    }





}
